/**
 * nghilt
 * 2011/09/18
 */
package vn.nnn.shogi.quanco;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JApplet;

import at.com.vn.chess.util.RectangleUtils;

import vn.nnn.shogi.board.Board;

/**
 * 
 */
public class QuanMa extends QuanCo {

	public QuanMa( Rectangle toaDo, int color, boolean normalDirection, JApplet mainApplet ) {
		super( toaDo, color, normalDirection, mainApplet );		
	}
	
	@Override
	public void setToaDoNhaTu() {
		toaDoNhaTu = new Point( 2, 2 );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canMove(java.awt.Rectangle, vn.nghi.test6.QuanCo, java.util.ArrayList)
	 */
	@Override
	public boolean canMove( Board board, Rectangle destination,
			QuanCo foundedPiece, ArrayList<QuanCo> listQuanCo ) {
		
		boolean canmove = false;
		
//		Rectangle quanThap;
//		Rectangle quanCao;
//		
////		if( color == QuanCo.QUAN_DEN ) {
//			quanThap = destination;
//			quanCao = toaDo;
////		}else {
////			quanThap = toaDo;
////			quanCao = destination;
////		}
			
			Rectangle quanThap = toaDo;
			Rectangle quanCao = destination;
			
			if( normalDirection ) {
				if( color == QUAN_DEN ) {
					quanThap = destination;
					quanCao = toaDo;
				}
			}else {
				if( color == QUAN_TRANG ) {
					quanThap = destination;
					quanCao = toaDo;
				}
			}
		
		if( RectangleUtils.isNextNextRow( quanThap, quanCao ) 
				&& RectangleUtils.isColumnBeside( quanThap, quanCao ) ) {
			canmove = true;
		}
		
		// Dieu kien co ban khi di quan
		if ( canmove ) {
			canmove = isPassToBasicMoveCondition( board, destination, foundedPiece, listQuanCo );
		}
		
		return canmove;
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#upgrade(java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public void upgrade( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		QuanMaPhongCap maPhongCap = new QuanMaPhongCap( destination, color, normalDirection, getMainApplet() );
		int idxChosen = getPositionInList( listQuanCo );
		
		listQuanCo.remove( idxChosen );
		listQuanCo.add( idxChosen, maPhongCap );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canRelease(vn.nnn.shogi.board.Board, java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public boolean canRelease( Board board, Rectangle destination,
			ArrayList<QuanCo> listQuanCo ) {
		// Neu khong vuot qua duoc dieu kien tha quan co ban
		if( !isPassToBasicReleaseCondition( board, destination, listQuanCo ) ) {
			return false;
		}		
		
		// Luat han che tha quan tot thu hai: khong duoc tha o hang cuoi cung
		if( board.isInVungCamThaQuanLoai2( destination, color ) ) {
			return false;
		}
		
		return true;
	}
	
	@Override
	public boolean isTrongVungBatBuocPhongQuan( Board board, Rectangle destination ) {
		if( board.isInVungCamThaQuanLoai2( destination, color ) ) {
			return true;
		}
		return false;
	}	
}
